<template>
  <el-dialog title="新增员工" :visible.sync="isShow">
    <el-form ref="form" label-width="200px" :model="form" :rules="rules">
      <el-form-item label="姓名：" prop="username">
        <el-input v-model="form.username" placeholder="1-30个字符" />
      </el-form-item>
      <el-form-item label="手机：" prop="mobile">
        <el-input v-model="form.mobile" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="入职时间：" prop="timeOfEntry">
        <el-date-picker
          v-model="form.timeOfEntry"
          type="date"
          placeholder="选择日期"
        />
      </el-form-item>
      <el-form-item label="聘用形式：" prop="formOfEmployment">
        <el-select v-model="form.formOfEmployment">
          <el-option
            v-for="i in employees.hireType"
            :key="i.id"
            :label="i.value"
            :value="i.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工号：" prop="workNumber">
        <el-input v-model="form.workNumber" placeholder="1-20个字符" />
      </el-form-item>
      <el-form-item label="部门：" prop="departmentName">
        <div v-clickoutside="clickoutside" class="department">
          <el-input
            v-model="form.departmentName"
            readonly
            placeholder="请选择"
            @focus="inputFocus"
          />
          <el-tree
            v-if="showTree"
            :data="treeData"
            :props="{ label: 'name' }"
            @node-click="nodeClick"
          />
        </div>
      </el-form-item>
      <el-form-item label="转正时间：" prop="correctionTime">
        <el-date-picker
          v-model="form.correctionTime"
          type="date"
          placeholder="选择日期"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <div style="text-align: center">
        <el-button type="primary" @click="submit">提 交</el-button>
        <el-button @click="isShow = false">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import employees from '@/assets/constant/employees.js'
import { companyDepartment } from '@/api/departments.js'
import { sysUserPost } from '@/api/employees.js'
export default {
  name: 'DialogItem',
  data() {
    return {
      isShow: false,
      employees: employees,
      form: {
        username: '',
        mobile: '',
        timeOfEntry: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        correctionTime: ''
      },
      rules: {
        username: [{ required: true, message: '必填', trigger: 'blur' }],
        mobile: [
          { required: true, message: '必填', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              const reg =
                /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              reg.test(value)
                ? callback()
                : callback(new Error('请输入正确手机号'))
            }
          }
        ],
        timeOfEntry: [{ required: true, message: '必填', trigger: 'blur' }],
        formOfEmployment: [
          { required: true, message: '必填', trigger: 'change' }
        ],
        workNumber: [{ required: true, message: '必填', trigger: 'blur' }],
        departmentName: [
          { required: true, message: '必填', trigger: 'change' }
        ],
        correctionTime: [{ required: true, message: '必填', trigger: 'blur' }]
      },
      treeData: [],
      showTree: false
    }
  },
  methods: {
    // 提交
    submit() {
      this.$refs.form.validate(async(result) => {
        if (result) {
          await sysUserPost(this.form)
          this.$message.success('新增员工成功')
          this.isShow = false
          this.$emit('pageChange', 1)
        }
      })
    },
    // 部门选择下拉框获取焦点
    async inputFocus() {
      this.showTree = true
      if (this.treeData.length === 0) {
        const res = await companyDepartment()
        this.treeData = this.changeCompanyDepartment(res.data.depts)
      }
    },
    // 修改部门数据结构
    changeCompanyDepartment(arr, str = '') {
      return arr.filter((item) => {
        if (item.pid === str) {
          item.children = this.changeCompanyDepartment(arr, item.id)
          return true
        }
      })
    },
    // 树形组件点击
    nodeClick(data) {
      this.form.departmentName = data.name
    },
    // 点击关闭树形组件
    clickoutside() {
      this.showTree = false
    }
  }
}
</script>

<style lang="scss" scoped>
.el-dialog {
  .el-input,
  .el-select,
  .department {
    width: 400px;
    position: relative;
    .el-tree {
      position: absolute;
      width: 400px;
      height: 160px;
      overflow: auto;
      border-radius: 4px;
      border: 1px solid #dcdfe6;
      z-index: 10;
    }
  }
}
</style>
